package com.manage.system.svc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.manage.common.core.constants.Constant;
import com.manage.common.core.result.JsonResult;
import com.manage.common.core.utils.PageUtil;
import com.manage.common.core.utils.StringUtils;
import com.manage.common.redis.generator.RedisIdGenerator;
import com.manage.system.api.entity.SysOperateLog;
import com.manage.system.api.request.log.OperateLogQueryParam;
import com.manage.system.api.service.ISysOperateLogService;
import com.manage.system.svc.mapper.SysOperateLogMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 操作日志 服务实现类
 * </p>
 *
 * @author zhangqing
 * @since 2021-07-17
 */
@Service
@AllArgsConstructor
@Slf4j
public class SysOperateLogServiceImpl extends ServiceImpl<SysOperateLogMapper, SysOperateLog> implements ISysOperateLogService {

    private final RedisIdGenerator redisIdGenerator;
    private final SysOperateLogMapper sysOperateLogMapper;

    @Override
    public void addLoginLog(SysOperateLog sysOperateLog) {
        sysOperateLog.setId(redisIdGenerator.nextId(Constant.SYS_OPERATE_LOG_KEY));
        this.baseMapper.insert(sysOperateLog);
    }

    @Override
    public JsonResult findOperateLogPage(OperateLogQueryParam operateLogQueryParam) {
        LambdaQueryWrapper<SysOperateLog> lambda = new QueryWrapper<SysOperateLog>().lambda()
                .eq(StringUtils.isNotNull(operateLogQueryParam.getStatus()),SysOperateLog::getStatus, operateLogQueryParam.getStatus())
                .eq(!StringUtils.isEmpty(operateLogQueryParam.getBusinessType()),SysOperateLog::getBusinessType, operateLogQueryParam.getBusinessType())
                .like(!StringUtils.isEmpty(operateLogQueryParam.getModule()),SysOperateLog::getModule, operateLogQueryParam.getModule())
                .eq(!StringUtils.isEmpty(operateLogQueryParam.getRequestType()),SysOperateLog::getRequestType, operateLogQueryParam.getRequestType())
                .like(!StringUtils.isEmpty(operateLogQueryParam.getOperator()),SysOperateLog::getOperator, operateLogQueryParam.getOperator())
                .orderByDesc(SysOperateLog::getCreateTime);

        if(!StringUtils.isEmpty(operateLogQueryParam.getBeginTime())&&!StringUtils.isEmpty(operateLogQueryParam.getEndTime())){
            lambda.ge(SysOperateLog::getCreateTime,operateLogQueryParam.getBeginTime())
                    .le(SysOperateLog::getCreateTime,operateLogQueryParam.getEndTime());
        }
        PageUtil.startPageSort(operateLogQueryParam.getPageNum(), operateLogQueryParam.getPageSize(),
                operateLogQueryParam.getSortField(), operateLogQueryParam.getSortOrder());
        List<SysOperateLog> sysOperateLogs = sysOperateLogMapper.selectList(lambda);
        return JsonResult.success(PageUtil.restPage(sysOperateLogs));
    }

    @Override
    public JsonResult delOneOperateLog(Long operateLogId) {
        log.info("系统服务->日志管理(操作日志)->根据id,删除单个操作日志->参数: id:{}",operateLogId);
        return JsonResult.toJsonResult(sysOperateLogMapper.deleteById(operateLogId));
    }

    @Override
    public JsonResult delAllOperateLog() {
        log.info("系统服务->日志管理(操作日志)->删除全部操作日志");
        return JsonResult.toJsonResult(sysOperateLogMapper.delete(Wrappers.emptyWrapper()));
    }

    @Override
    public JsonResult batchDelOperateLog(List<Long> ids) {
        log.info("系统服务->日志管理(操作日志)->批量删除操作日志->参数: ids:{}",ids.toString());
        return JsonResult.toJsonResult(sysOperateLogMapper.deleteBatchIds(ids));
    }
}
